package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.module.system.mapper.SysUserMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysUserFeedbackRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysUserFeedbackMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysUserFeedback;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysUserFeedbackService;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户反馈业务层处理
 *
 * @author mars
 * @date 2024-04-15
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysUserFeedbackServiceImpl implements ISysUserFeedbackService {

    private final SysUserFeedbackMapper baseMapper;
    private final SysUserMapper userMapper;

    private String generateOrderNumber(String pre) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return pre + sdf.format(new Date());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysUserFeedback add(SysUserFeedbackRequest request) {
        SysUserFeedback entity = SysUserFeedback.builder().build();
        BeanUtils.copyProperties(request, entity);
        Long userId = ContextUserInfoThreadHolder.get().getId();
        entity.setCreateById(userId);
        entity.setSerialNo(generateOrderNumber("FK"));
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysUserFeedbackRequest request) {
        SysUserFeedback entity = SysUserFeedback.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysUserFeedback getById(Long id) {
        SysUserFeedback feedback = baseMapper.selectById(id);
        feedback.setUserName(userMapper.selectById(feedback.getUserId()).getUserName());
        return feedback;
    }

    @Override
    public PageInfo<SysUserFeedback> pageList(SysUserFeedbackRequest request) {
        Page<SysUserFeedback> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysUserFeedback> query = this.buildWrapper(request);
        IPage<SysUserFeedback> pageRecord = baseMapper.selectPage(page, query);
        List<SysUserFeedback> records = pageRecord.getRecords();
        List<SysUserFeedback> recordsNew = new ArrayList<>();
        for (SysUserFeedback feedback : records) {
            feedback.setUserName(userMapper.selectById(feedback.getUserId()).getUserName());
            recordsNew.add(feedback);
        }
        if (request.getUserName() != null) {
            recordsNew = recordsNew.stream().filter(e -> e.getUserName().equals(request.getUserName())).collect(Collectors.toList());
        }
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysUserFeedback> list(SysUserFeedbackRequest request) {
        LambdaQueryWrapper<SysUserFeedback> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysUserFeedback> buildWrapper(SysUserFeedbackRequest param) {
        LambdaQueryWrapper<SysUserFeedback> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getSerialNo())) {
            query.like(SysUserFeedback::getSerialNo, param.getSerialNo());
        }
        if (StringUtils.isNotBlank(param.getFeedbackContent())) {
            query.like(SysUserFeedback::getFeedbackContent, param.getFeedbackContent());
        }
        if (StringUtils.isNotBlank(param.getReplayContent())) {
            query.like(SysUserFeedback::getReplayContent, param.getReplayContent());
        }
        return query;
    }

}
